package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.*;
import com.example.hcbmsm.mapper.InvMapper;
import com.example.hcbmsm.service.InboundService;
import com.example.hcbmsm.service.InvService;
import com.example.hcbmsm.service.OutboundService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/11/1 16:42
 */
@Service
public class InvServiceImpl implements InvService {
    @Autowired
    private InvMapper invMapper;
    @Autowired
    private OutboundService outboundService;
    @Autowired
    private InboundService inboundService;

    @Override
    public Map renew(Inv inv) {
        inv.setModifyTime(new Date());
        if (invMapper.updateByPrimaryKeySelective(inv) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Inv getDrugAmount(Long drugId) {
        InvExample invExample = new InvExample();
        invExample.createCriteria().andDrugIdEqualTo(drugId);
        return invMapper.selectByExample(invExample).get(0);
    }

    @Override
    public void removeByDrugId(Long drugId) {
        InvExample invExample = new InvExample();
        invExample.createCriteria().andDrugIdEqualTo(drugId);
        invMapper.deleteByExample(invExample);
    }

    @Override
    public void addInv(Inv inv) {
        invMapper.insertSelective(inv);
    }

    @Override
    public Map list(Long drugId, int pageNo, int limit) {
        Integer total = invMapper.selectByDrugId(drugId).size();
        PageHelper.startPage(pageNo, limit);
        List<Inv> list = invMapper.selectByDrugId(drugId);
        Map map = new HashMap();
        map.put("total", total);
        map.put("data", list);
        return HCBMSMResult.SUCCESS(map);
    }

    @Override
    public Map inAndOutInv(String token, Inv inv) {
        Inv oldInv = invMapper.selectByPrimaryKey(inv.getInvId());
        inv.setModifyTime(new Date());
        if (invMapper.updateByPrimaryKeySelective(inv) == 1) {
            Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
            Long docId = new Long(tokenInfo.get("docId").asString());
            if (oldInv.getInvAmount() > inv.getInvAmount()) {
                Outbound outbound = new Outbound();
                outbound.setInvId(inv.getInvId());
                outbound.setDocId(docId);
                outbound.setOutboundNumber(oldInv.getInvAmount() - inv.getInvAmount());
                outboundService.add(outbound);
            } else {
                Inbound inbound = new Inbound();
                inbound.setInvId(inv.getInvId());
                inbound.setDocId(docId);
                inbound.setInboundNumber(inv.getInvAmount() - oldInv.getInvAmount());
                inboundService.add(inbound);
            }
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
